﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ext.Net;
using System.Xml.Xsl;
using System.Xml;

namespace WEB.Core.Controls
{
    public partial class BasePage
    {
        #region Export
        protected void ExportXML(XmlNode XML, string FileName)
        {
            Response.Clear();

            string strXml = XML.OuterXml;
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.xml", FileName));
            Response.AddHeader("Content-Length", strXml.Length.ToString());
            Response.ContentType = "application/xml";
            Response.Write(strXml);

            Response.End();
        }

        protected void ExportExcel(XmlNode XML, string XSLFileName, string FileName)
        {
            Response.Clear();

            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.xls", FileName));
            XslCompiledTransform xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath(String.Format(@"\ExportXSL\Excel\{0}.xsl", XSLFileName)));
            xtExcel.Transform(XML, null, Response.OutputStream);

            Response.End();
        }

        protected void ExportCSV(XmlNode XML, string FileName)
        {
            Response.Clear();

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.csv", FileName));
            XslCompiledTransform xtCsv = new XslCompiledTransform();
            xtCsv.Load(Server.MapPath(@"\ExportXSL\CSV.xsl"));
            xtCsv.Transform(XML, null, Response.OutputStream);

            Response.End();
        }
        #endregion

        //#region Element
        //private char _CharSplitHidden = '۞';
        //protected virtual void SaveInfoExport(params string[] Values)
        //{
        //    GetHidden("hdInfoExport").Text = string.Join(_CharSplitHidden.ToString(), Values);
        //}
        //protected virtual void SaveReportExport(params string[] Values)
        //{
        //    GetHidden("hdReportExport").Text = string.Join(_CharSplitHidden.ToString(), Values);
        //}

        //protected virtual XmlElement CreateEleExport(XmlDocument doc, string ElementName, string[] Name, string[] Value)
        //{
        //    XmlElement _EleRecord = doc.CreateElement(ElementName);
        //    XmlElement _EleValue;
        //    int _minLength = Math.Min(Name.Length, Value.Length);
        //    for (int i = 0; i < _minLength; i++)
        //    {
        //        _EleValue = doc.CreateElement(Name[i]);
        //        _EleValue.InnerText = Value[i];
        //        _EleRecord.AppendChild(_EleValue);
        //    }
        //    return _EleRecord;
        //}
        //protected virtual XmlElement TempCreateEleInfoExport(XmlDocument doc, params string[] Name)
        //{
        //    return CreateEleExport(doc, "Info", GetHidden("hdInfoExport").Text.Split(_CharSplitHidden), Name);
        //}
        //protected virtual XmlElement TempCreateEleReportExport(XmlDocument doc, params string[] Name)
        //{
        //    return CreateEleExport(doc, "Report", GetHidden("hdReportExport").Text.Split(_CharSplitHidden), Name);
        //}
        //#endregion
    }
}
